package com.runvp.service.tcp;

import com.alibaba.fastjson.JSONObject;
import com.runvp.common.core.redis.RedisCache;
import com.runvp.common.model.tcp.MessageParseBody;
import com.runvp.service.app.AppClientService;
import com.runvp.service.biz.domain.BizTcpRecord;
import com.runvp.service.biz.service.*;
import com.runvp.service.tcp.dao.JdbcDao;
import com.runvp.system.service.ISysConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Slf4j
@Service
public class TcpService {

    @Autowired
    private JdbcDao jdbcDao;
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private IBizDeviceService bizDeviceService;

    @Autowired
    private IBizDeviceCargoService bizDeviceCargoService;
    @Autowired
    private IBizOrderService bizOrderService;
    @Autowired
    private IBizCustomerService bizCustomerService;
    @Autowired
    private IBizCustomerWalletService bizCustomerWalletService;
    @Autowired
    private ISysConfigService configService;
    @Autowired
    private IBizGoodsService bizGoodsService;
    @Autowired
    private AppClientService appClientService;

    @Autowired
    private IBizTcpRecordService bizTcpRecordService;

    public IBizTcpRecordService instanceIBizTcpRecordService(){
        return bizTcpRecordService;
    }

    public AppClientService instanceAppClientService(){
        return appClientService;
    }

    public IBizGoodsService instanceIBizGoodsService(){
        return bizGoodsService;
    }

    public ISysConfigService instanceISysConfigService(){
        return configService;
    }

    public IBizCustomerService instanceIBizCustomerService(){
        return bizCustomerService;
    }
    public IBizCustomerWalletService instanceIBizCustomerWalletService(){
        return bizCustomerWalletService;
    }

    public IBizOrderService instanceIBizOrderService(){
        return bizOrderService;
    }
    public IBizDeviceService instanceIBizDeviceService(){
        return bizDeviceService;
    }
    public IBizDeviceCargoService instanceIBizDeviceCargoService(){
        return bizDeviceCargoService;
    }

    public RedisCache instanceRedisUtil(){
        return redisCache;
    }
    /**
     * 获取 OrderService
     * @return OrderService
     */
    public JdbcDao instanceJdbcDao(){
        return jdbcDao;
    }

    /**
     * 保存tcp通讯记录
     * @param obj
     */
    public void addBizTcpRecord(Object obj){
        try{
            String jsonBody = JSONObject.toJSONString(obj);
//            log.info("addBizTcpRecord:::add:{} ", jsonBody);
            MessageParseBody add = JSONObject.parseObject(jsonBody,MessageParseBody.class);
            BizTcpRecord record = new BizTcpRecord();
            record.setSn(add.getTerminalId());
            record.setCreateTime(new Date());
            record.setBatchNo(add.getBatchNo());
            record.setTcpType(add.getType());
            record.setCmdType(add.getCmd());
            record.setSource(add.getSourceMsg());
            record.setContent(jsonBody);
            bizTcpRecordService.insertBizTcpRecord(record);
        }catch (Exception e){
            log.error("保存tcp通讯记录报错",e);
        }

    }

}
